package cn.sinata.wanglin.utils;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xilada.xldutils.fragment.BaseLazyFragment;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.view.SwipeRefreshRecyclerLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import cn.sinata.wanglin.R;

/**
 *
 * Created by liaoxiang on 16/1/18.
 */
public abstract class RecyclerViewFragmentWithNoAuth extends BaseLazyFragment{

    private SwipeRefreshRecyclerLayout mSwipeRefreshLayout;
    private RecyclerView.ItemDecoration defaultItemDecoration;
    private TextView tv_gotoauth;
    private RelativeLayout layout_noauth;
    protected int DEFAULT_MARGIN_DIVIDER = 0;
    protected int DEFAULT_DIVIDER = 1;
    private boolean isShowAuthView=false;
    protected abstract RecyclerView.Adapter setAdapter();
    protected void noAuthOnClickListener(){};
    /**
     * 下拉刷新
     */
    protected void pullDownRefresh(){}

    /**
     * 加载更多
     */
    protected void loadMore(){}

    /**
     * @return 默认上拉下拉同时存在
     */
    protected SwipeRefreshRecyclerLayout.Mode getMode(){
        return SwipeRefreshRecyclerLayout.Mode.Both;
    }

    /**
     * @return  默认垂直布局。
     */
    protected RecyclerView.LayoutManager getLayoutManager(){
        return new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_vertical_linear_recyclerview_withscroll;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipeRefreshLayout = mHolder.bind(R.id.mSwipeRefreshLayout);
        layout_noauth = mHolder.bind(R.id.layout_noauth);
        layout_noauth.setVisibility(View.GONE);
        tv_gotoauth = mHolder.bind(R.id.tv_gotoauth);
        tv_gotoauth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                noAuthOnClickListener();
            }
        });

        defaultItemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                .colorResId(R.color.dividing_line_color_wanglin)
                .marginResId(R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin)
                .size(10)
                .build();
        //默认分割线
        setItemDecoration(DEFAULT_DIVIDER);
        mSwipeRefreshLayout.setLayoutManager(getLayoutManager());
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshRecyclerLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pullDownRefresh();
            }

            @Override
            public void onLoadMore() {
                loadMore();
            }
        });
        mSwipeRefreshLayout.setMode(getMode());
        if (setAdapter() != null) {
            final RecyclerView.Adapter adapter = setAdapter();

            mSwipeRefreshLayout.setAdapter(adapter);
            dataObserver = new RecyclerView.AdapterDataObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    RecyclerView.Adapter a = mSwipeRefreshLayout.getAdapter();
                    if (a == null) {
                        return;
                    }
                    if (isShowAuthView){
                        layout_noauth.setVisibility(View.VISIBLE);
                        mSwipeRefreshLayout.setVisibility(View.GONE);
                    }else {
                        layout_noauth.setVisibility(View.GONE);
                        mSwipeRefreshLayout.setVisibility(View.VISIBLE);
                    }
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            };
            adapter.registerAdapterDataObserver(dataObserver);
        }
    }

    RecyclerView.AdapterDataObserver dataObserver ;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RecyclerView.Adapter adapter = mSwipeRefreshLayout.getAdapter();
        if (dataObserver!=null && adapter!=null){
            try {
                adapter.unregisterAdapterDataObserver(dataObserver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 设置刷新状态
     * @param refreshing 刷新状态
     */
    public void setRefreshing(boolean refreshing){
        mSwipeRefreshLayout.setRefreshing(refreshing);
    }

    /**
     * 刷新完成
     */
    public void onRefreshComplated(){
        mSwipeRefreshLayout.onRefreshCompleted();
    }

    protected void setLoadMoreText(CharSequence text){
        mSwipeRefreshLayout.setLoadMoreText(text);
    }

    /**
     * 设置分割线,只能设置1种分割线
     * @param itemDecoration 样式
     */
    protected void setItemDecoration(RecyclerView.ItemDecoration itemDecoration){
        if (itemDecoration == null){
            mSwipeRefreshLayout.addItemDecoration(defaultItemDecoration);
            return;
        }
        if (defaultItemDecoration != null) {
            mSwipeRefreshLayout.removeItemDecoration(defaultItemDecoration);
        }
        defaultItemDecoration = itemDecoration;
        mSwipeRefreshLayout.addItemDecoration(defaultItemDecoration);
    }

    /**
     * 添加分割线，可以是多种。比如网格的横竖2中分割线，不会覆盖。
     * @param itemDecoration 分割线
     */
    protected void addItemDecoration(RecyclerView.ItemDecoration itemDecoration){
        if (itemDecoration==null){
            return;
        }
        mSwipeRefreshLayout.addItemDecoration(itemDecoration);
    }

    protected void setItemDecoration(int type) {
        HorizontalDividerItemDecoration itemDecoration;
        if (type == DEFAULT_MARGIN_DIVIDER){
            itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .margin(DensityUtil.dip2px(context,16))
                    .size(1)
                    .color(ContextCompat.getColor(context,R.color.dividing_line_color))
                    .build();

        }else {
            itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .size(DensityUtil.dip2px(context,10))
                    .color(ContextCompat.getColor(context,R.color.dividing_line_color))
                    .build();
        }
        setItemDecoration(itemDecoration);
    }

    public boolean isShowAuthView() {
        return isShowAuthView;
    }

    public void setShowAuthView(boolean showAuthView) {
        isShowAuthView = showAuthView;
    }

    public RecyclerView getRecyclerView(){
       return mSwipeRefreshLayout.getRecyclerView();
    }
}